<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\user\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\user\model\Template as TemplateModel;

/**
 * 演示文稿控制器
 * @package app\user\admin
 */
class Document extends Admin
{
    /**
     * 演示文稿列表
     * @author 李洪波 <361951119@qq.com>
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $data_list = TemplateModel::where($this->getMap())
            ->order($this->getOrder('id DESC'))
            ->paginate();

        return ZBuilder::make('table')
            ->setTableName('admin_template')
            ->addTopButton('add')
            ->addTopButton('delete')
            ->addRightButton('edit')
            ->addRightButton('delete')
            ->addColumns([
                ['id', 'ID'],
                ['name', '标题'],
                ['image_1', '封面1','picture'],
                ['status', '状态', 'switch'],
                ['create_time', '创建时间'],
                ['right_button', '操作', 'btn'],
            ])
            ->addFilter('status',[1=>'启用',0=>'禁用'])
            ->setColumnWidth([
                'id'=>30])
            ->setRowList($data_list)
            ->fetch();
    }

    /**
     * 新增
     * @author 李洪波 <361951119@qq.com>
     * @return mixed
     * @throws \think\Exception
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['name'] == '' && $this->error('请填写标题');
            $data['title_en_1'] == '' && $this->error('请至少填写tab1 英文内容');
            $data['image_1'] == '' && $this->error('请至少上传tab1 图片');
            $list = $data;
            unset($list['__token__']);
            $list['create_time']=date('Y-m-d H:i:s');
            $TemplateModel = new TemplateModel();
            if (false !== $TemplateModel->save($list)) {
                $this->success('新增成功', 'index');
            } else {
                $this->error('新增失败');
            }
        }
        $number=config('setting.tab_number');
        $tabs=[];
        for($i=1;$i<=$number;$i++){
            $formItems=[];
            $languages=get_languages();
            foreach ($languages as $key=>$language){
                $formItems[]=['ueditor','title_'.$language['sign'].'_'.$i,$language['name'].'内容'];
            }
            $formItems[]=['image','image_'.$i,'图片'];
            $tabs['tab'.$i]=$formItems;
        }

        return ZBuilder::make('form')
            ->addFormItems([
                ['text', 'name', '标题'],
            ])
            ->addGroup(
                $tabs
            )
            ->fetch();
    }

    public function edit($id = 0)
    {
        if ($id === 0) $this->error('缺少参数');

        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post('', null, 'trim');

            // 验证
            $data['name'] == '' && $this->error('请填写标题');
            $data['title_en_1'] == '' && $this->error('请至少填写tab1 英文内容');
            $data['image_1'] == '' && $this->error('请至少上传tab1 图片');

            if (TemplateModel::update($data,['id'=>$id])) {
                $this->success('编辑成功', 'index');
            } else {
                $this->error('编辑失败');
            }
        }

        // 获取数据
        $info = TemplateModel::get($id);
        $number=config('setting.tab_number');
        $tabs=[];
        for($i=1;$i<=$number;$i++){
            $formItems=[];
            $languages=get_languages();
            foreach ($languages as $key=>$language){
                $formItems[]=['ueditor','title_'.$language['sign'].'_'.$i,$language['name'].'内容'];
            }
            $formItems[]=['image','image_'.$i,'图片'];
            $tabs['tab'.$i]=$formItems;
        }
        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->addFormItems([
                ['text', 'name', '标题'],
            ])
            ->addGroup(
                $tabs
            )
            ->setFormData($info)
            ->fetch();
    }
}
